package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.service.MarketingManagementService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.listbatch.pojo.dto.OnlineDTO;
import com.xbongbong.pro.marketing.pojo.dto.MarketingManagementAddGetDTO;
import com.xbongbong.pro.marketing.pojo.dto.MarketingManagementDTO;
import com.xbongbong.pro.marketing.pojo.dto.MarketingManagementDataAddDTO;
import com.xbongbong.pro.marketing.pojo.dto.MarketingManagementDataCopyDTO;
import com.xbongbong.pro.marketing.pojo.dto.MarketingManagementDataGetDTO;
import com.xbongbong.pro.marketing.pojo.dto.MarketingManagementDataUpdateDTO;
import com.xbongbong.pro.marketing.pojo.dto.MarketingManagementDataUpdateGetDTO;
import com.xbongbong.pro.marketing.pojo.dto.MarketingManagementDeleteBatchDTO;
import com.xbongbong.pro.marketing.pojo.vo.MarketingManagementAddGetVO;
import com.xbongbong.pro.marketing.pojo.vo.MarketingManagementDataAddVO;
import com.xbongbong.pro.marketing.pojo.vo.MarketingManagementDataCopyVO;
import com.xbongbong.pro.marketing.pojo.vo.MarketingManagementDataGetVO;
import com.xbongbong.pro.marketing.pojo.vo.MarketingManagementDataUpdateGetVO;
import com.xbongbong.pro.marketing.pojo.vo.MarketingManagementDataUpdateVO;
import com.xbongbong.pro.marketing.pojo.vo.MarketingManagementDeleteBatchVO;
import com.xbongbong.pro.marketing.pojo.vo.MarketingManagementVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author jyh
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/marketing/activity")
public class MobileMarketingManagementController {


    @Resource
    private MarketingManagementService marketingManagementService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid MarketingManagementDTO marketingManagementDTO, BindingResult br) throws Exception {
        XbbResponse<MarketingManagementVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MarketingManagementVO marketingManagementVO = marketingManagementService.getActivityList(marketingManagementDTO);
                response = new XbbResponse<>(marketingManagementVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    @RequestMapping(value = "/add/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addGet(@RequestBody @Valid MarketingManagementAddGetDTO marketingManagementDTO, BindingResult br) throws Exception {
        XbbResponse<MarketingManagementAddGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MarketingManagementAddGetVO marketingManagementVO = marketingManagementService.addGet(marketingManagementDTO);
                response = new XbbResponse<>(marketingManagementVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/data/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid MarketingManagementDataAddDTO marketingManagementDTO, BindingResult br) throws Exception {
        XbbResponse<MarketingManagementDataAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(marketingManagementDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(marketingManagementDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                MarketingManagementDataAddVO marketingManagementVO = marketingManagementService.add(marketingManagementDTO);
                response = new XbbResponse<>(marketingManagementVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/data/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid MarketingManagementDataGetDTO marketingManagementDTO, BindingResult br) throws Exception {
        XbbResponse<MarketingManagementDataGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(marketingManagementDTO.getSaasMark(), marketingManagementDTO.getBusinessType(), marketingManagementDTO.getMenuId(), marketingManagementDTO.getDistributorMark());
                MarketingManagementDataGetVO marketingManagementVO = marketingManagementService.get(marketingManagementDTO);
                response = new XbbResponse<>(marketingManagementVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/update/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateGet(@RequestBody @Valid MarketingManagementDataUpdateGetDTO marketingManagementDTO, BindingResult br) throws Exception {
        XbbResponse<MarketingManagementDataUpdateGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MarketingManagementDataUpdateGetVO marketingManagementVO = marketingManagementService.updateGet(marketingManagementDTO);
                response = new XbbResponse<>(marketingManagementVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    @RequestMapping(value = "/copy", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateGet(@RequestBody @Valid MarketingManagementDataCopyDTO marketingManagementDTO, BindingResult br) throws Exception {
        XbbResponse<MarketingManagementDataCopyVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MarketingManagementDataCopyVO marketingManagementVO = marketingManagementService.copy(marketingManagementDTO);
                response = new XbbResponse<>(marketingManagementVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid MarketingManagementDataUpdateDTO marketingManagementDTO, BindingResult br) throws Exception {
        XbbResponse<MarketingManagementDataUpdateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MarketingManagementDataUpdateVO marketingManagementVO = marketingManagementService.update(marketingManagementDTO);
                response = new XbbResponse<>(marketingManagementVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/deleteSingle", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid MarketingManagementDeleteBatchDTO marketingManagementDTO, BindingResult br) throws Exception {
        XbbResponse<MarketingManagementDeleteBatchVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MarketingManagementDeleteBatchVO marketingManagementVO = marketingManagementService.deleteBatch(marketingManagementDTO);
                response = new XbbResponse<>(marketingManagementVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }



    @RequestMapping(value = "/online", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String online(@RequestBody @Valid OnlineDTO onlineDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO online = marketingManagementService.online(onlineDTO);
                response = new XbbResponse<>(online);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


}
